/**
 * h5 plus 助手类
 * @method getAndroidID()
 * @method getOAID()
 */
class EsPlusUtils {
	static enable() {
		console.log('[EsPlusUtils] enable plus:typeof=', typeof(plus));
		return typeof(plus) == 'object';

	}
	static getAndroidID() {
		if (!EsPlusUtils.enable()) {
			return null;
		}
		let value = uni.getStorageSync('ANDROID_ID');
		if (value) {
			return value;
		}
		// 获取 ANDROID_ID
		let Settings = plus.android.importClass("android.provider.Settings");
		let main = plus.android.runtimeMainActivity();
		value = Settings.Secure.getString(main.getContentResolver(), Settings.Secure.ANDROID_ID);
		uni.setStorageSync('ANDROID_ID', value); // 29681e7828b0de2e
		console.log('[EsPlusUtils] androidId=', value);
		return value;
	}
	static async getOAID(defaultVal = null) {
		// 获取 OAID（Android 10+，属于广告联盟的，仅在华为、OPPO、VIVO、小米、联想等国产手机设备支持。）
		if (!EsPlusUtils.enable()) {
			return defaultVal;
		}
		let value = uni.getStorageSync('OAID');
		if (value) {
			console.log('[EsPlusUtils] getOAID.value=', value);
			return value;
		}
		console.log('[EsPlusUtils] getOAID.xxx2');
		let result = await new Promise(function(reslove, reject) {
			plus.device.getOAID({
				success: function(e) {
					console.log('getOAID:success1=', e);
					reslove(e, true);
				},
				fail: function(e) {
					console.log('getOAID:fail22=', e);
					reslove(e, false);
				}
			});
		});
		console.log('[EsPlusUtils] getOAID.result=', result);
		if (typeof(result.oaid) != 'undefined') {
			uni.setStorageSync('OAID', result.oaid); // oaid: "fa68bf4207739fd5"			
		}
		return result.oaid || defaultVal;
	}
	/**
	 * 获取APP信息(版本号和应用名称)
	 * widgetInfo.version, widgetInfo.name
	 */
	async getWidgetInfo() {
		const widgetInfo = await new Promise(function(reslove, reject) {
			plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
				reslove(widgetInfo);
			});
		})
		return widgetInfo;
	}
	// 获取设备信息
	getDeviceInfo(fnSuccess) {
		if (!EsPlusUtils.enable()) {
			return null;
		}
		plus.device.getInfo({
			success: function(e) {
				uni.setStorageSync('device_info', e);
				console.log('[EsPlusUtils] getDeviceInfo success: ' + JSON.stringify(e));
				fnSuccess(e);
			},
			fail: function(e) {
				console.log('[EsPlusUtils] getDeviceInfo failed: ' + JSON.stringify(e));
			}
		});
	}
}
export {
	EsPlusUtils
}
